package com.byit.Test.Mytest1.ProducerAndConsumer;

import java.util.LinkedList;

/**
 * @ClassName Repertory
 * @Description 仓库 存放商品
 * @Author lxp
 * @Date 2018/10/9 0009
 **/
public class Repertory {


    public LinkedList<Product> store = new LinkedList<Product>();

    public LinkedList<Product> getStore() {
        return store;
    }

    public void setStore(LinkedList<Product> store) {
        this.store = store;
    }

    /**
     * 仓库存放商品的方法
     *
     * @param p
     * @param threadName
     */
    public synchronized void push(Product p, String threadName) {


        while (store.size() == 10) {
            System.out.println(threadName + "报告:仓库已满--->进入等待状态--->呼叫老大过来消费");
            try {
                /**
                 * 仓库商品数量=10等待
                 */
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        /**
         * 仓库商品数量<10继续生产
         */
        this.notifyAll();
        store.addLast(p);
        System.out.println(threadName + "生产了:" + p.id + "号产品" + " " + "当前库存量:" + store.size());
        try {
            /**
             * 方便观察
             */
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    /**
     * 消费者方法
     *
     * @param threadName
     */
    public synchronized void pop(String threadName) {

        while (store.size() == 0) {
            try {
                /**
                 * 如果仓库商品数量小于10线程等待
                 */
                System.out.println(threadName+"下命令:仓库已空--->进入等待状态--->命令小弟赶快生产");
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        this.notifyAll();
       // store.removeFirst();
        System.out.println(threadName + "消费了:" + store.removeFirst().id + "号产品" + " " + "当前库存量:" + store.size());

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
